<?php
class evento{
	private $id;
	private $id_protocolo;
	private $id_despachante;
        private $id_destinatario;
	private $id_status;
	private $dt_despacho;
	private $hr_despacho;
        private $despachante;
        private $status;
	private $descricao;
        private $lido;

	public function __construct(){
		$this->id = NULL;
		$this->id_protocolo = NULL;
		$this->id_despachante = NULL;
                $this->id_destinatario = NULL;
		$this->id_status = NULL;
		$this->dt_despacho = date("Y-m-d");
		$this->hr_despacho = date("H:i:s");
		$this->despachante = NULL;
                $this->status = NULL;
                $this->descricao = NULL;
                $this->lido = 0;
	}

	public function get_id(){
		return $this->id;
	}
	public function get_id_protocolo(){
		return $this->id_protocolo;
	}
	public function get_id_despachante(){
		return $this->id_despachante;
	}
        public function get_despachante(){
            /*
                $sql = "select nome from ci_despachante where id = '".$this->id_despachante."'";
                $db = new db(config::$driver);
                $desp = $db->select($sql);
		return $desp[0][0];
                */
            return $this->despachante;
	}
        public function get_id_destinatario(){
		return $this->id_destinatario;
	}
        public function get_destinatario(){
                $sql = "select nome from ci_despachante where id = '".$this->id_destinatario."'";
                $db = new db(config::$driver);
                $desp = $db->select($sql);
		return $desp[0][0];
	}
	public function get_id_status(){
		return $this->id_status;
	}
        public function get_status(){
            /*
                $sql = "select descricao from ci_status where id = '".$this->id_status."'";
                $db = new db(config::$driver);
                $desp = $db->select($sql);
		return $desp[0][0];
                */
            return $this->status;
	}
	public function get_dt_despacho(){
                $ano = substr($this->dt_despacho, 0, 4);
		$mes = substr($this->dt_despacho, 5, 2);
		$dia = substr($this->dt_despacho, 8, 2);
		$r = $dia."/".$mes."/".$ano;
		return $r;
	}
	public function get_hr_despacho(){
		return $this->hr_despacho;
	}
	public function get_descricao(){
		return $this->descricao;
	}
        public function get_lido(){
		return $this->lido;
	}
	
	
	public function set_id($id){
		if($id != '' && valida::numero($id)){
                    $this->id = $id;
                    return 1;
		}else{
                    return 0;
                }
        }

	public function set_id_protocolo($id_protocolo){
		if($id_protocolo != '' && valida::numero($id_protocolo)){
                    $this->id_protocolo = $id_protocolo;
                    return 1;
		}else{
                    return 0;
                }
        }

	public function set_id_despachante($id_despachante){
		if($id_despachante != '' && valida::numero($id_despachante)){
                    $this->id_despachante = $id_despachante;
                    return 1;
		}else{
                    return 0;
                }
        }
        
        public function set_id_destinatario($id_destinatario){
		if($id_destinatario != '' && valida::numero($id_destinatario)){
                    $this->id_destinatario = $id_destinatario;
                    return 1;
		}else{
                    return 0;
                }
        }

	public function set_id_status($id_status){
		if($id_status != '' && valida::numero($id_status)){
                    $this->id_status = $id_status;
                    return 1;
		}else{
                    return 0;
                }
        }

	public function set_dt_despacho($dt_despacho){
		if($dt_despacho != '' && valida::data($dt_despacho)){
                    $this->dt_despacho = $dt_despacho;
                    return 1;
		}else{
                    return 0;
                }
        }

	public function set_hr_despacho($hr_despacho){
                $hr_despacho = addslashes($hr_despacho);
		if($hr_despacho != ''){
                    $this->hr_despacho = $hr_despacho;
                    return 1;
		}else{
                    return 0;
                }
        }

	public function set_descricao($descricao){
                $descricao = addslashes($descricao);
                $this->descricao = $descricao;
                return 1;
        }
        public function set_lido($lido){
            if(valida::numero($lido)){
                $this->lido = $lido;
                return 1;
            }else{
                return 0;
            }
        }

	
	
	public function salvar(){
            
            $db = new db(config::$driver);
            $con = $db->conecta();
            
            if(isset($this->id)){
                $sql = "update ci_evento set id_protocolo = '".$this->id_protocolo."', id_despachante = '".$this->id_despachante."', id_status = '".$this->id_status."', dt_despacho = '".$this->dt_despacho."', hr_despacho = '".$this->hr_despacho."', descricao = '".$this->descricao."', id_destinatario = '".$this->id_destinatario."', lido = '".$this->lido."' where id = '$this->id'";
                $res = $db->query($sql, $con);
                $id = $this->id;
            }else{
                $sql = "insert into ci_evento(id_protocolo, id_despachante, id_destinatario, id_status, dt_despacho, hr_despacho, descricao, lido) values('".$this->id_protocolo."', '".$this->id_despachante."', '".$this->id_destinatario."', '".$this->id_status."', '".$this->dt_despacho."', '".$this->hr_despacho."', '".$this->descricao."', '".$this->lido."')";
                $res = $db->query($sql, $con);
                
                $sql_id = "select max(id) from ci_evento";
                $res_id = $db->query($sql_id, $con);
                $id_res = $db->fetch_array($res_id);
                $id = $id_res;
            }
            
            $db->close($con);

            return $id;
	}
        
	public function carregar($id){
		$sql = "select e.*, d.nome as despachante, s.descricao as status from ci_evento e, ci_despachante d, ci_status s
where e.id_despachante = d.id and e.id_status = s.id and e.id = '$id'";
                $db = new db(config::$driver);
		$con = $db->conecta();
		$res = $db->query($sql, $con);
		$db->close($con);
		$ci_evento = $db->fetch_array($res);

		$this->id = $ci_evento["id"];
		$this->id_protocolo = $ci_evento["id_protocolo"];
		$this->id_despachante = $ci_evento["id_despachante"];
                $this->id_destinatario = $ci_evento["id_destinatario"];
		$this->id_status = $ci_evento["id_status"];
		$this->dt_despacho = $ci_evento["dt_despacho"];
		$this->hr_despacho = $ci_evento["hr_despacho"];
		$this->descricao = $ci_evento["descricao"];
                $this->despachante = $ci_evento["despachante"];
                $this->status = $ci_evento["status"];
                $this->lido = $ci_evento["lido"];
		
		return 1;
	}
        
        public function carregar_ultimo($id_protocolo){
            $sql = "select e.*, d.nome as despachante, s.descricao as status from ci_evento e, ci_despachante d, ci_status s
where e.id_despachante = d.id and e.id_status = s.id
and e.id_protocolo = '$id_protocolo' order by e.id desc limit 1";
            $db = new db(config::$driver);
            $con = $db->conecta();
            $res = $db->query($sql, $con);
            $db->close($con);
            $ci_evento = $db->fetch_array($res);

            $this->id = $ci_evento["id"];
            $this->id_protocolo = $ci_evento["id_protocolo"];
            $this->id_despachante = $ci_evento["id_despachante"];
            $this->id_destinatario = $ci_evento["id_destinatario"];
            $this->id_status = $ci_evento["id_status"];
            $this->dt_despacho = $ci_evento["dt_despacho"];
            $this->hr_despacho = $ci_evento["hr_despacho"];
            $this->descricao = $ci_evento["descricao"];
            $this->despachante = $ci_evento["despachante"];
            $this->status = $ci_evento["status"];
            $this->lido = $ci_evento["lido"];

            return 1;
        }
}
?>
